package com.example.restatr.Atcivity.Main.Fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.restatr.Adapter.ViewPagerAdapter;
import com.example.restatr.Adapter.NewsListAdapter;
import com.example.restatr.Bean.NewsBean;
import com.example.restatr.Atcivity.Main.NullAt;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.example.restatr.Atcivity.NewsAt;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class PartyBuildFragment extends Fragment {
    private ViewPager djBanner;
    private GridView gv;
    private LinearLayout djMore;
    private RecyclerView djXw;
    private List<ImageView> banner = new ArrayList<>();
    private View yuan1;
    private View yuan2;
    private View yuan3;
    private Timer timer = new Timer();
    private int pos;
    private List<Integer> icons = new ArrayList<>();
    private List<String> titles = new ArrayList<>();
    private ArrayList<NewsBean.RowsBean> objects = new ArrayList<>();

    private NewsListAdapter xwListAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate( R.layout.fragment_dj, container, false );

        djBanner = view.findViewById( R.id.dj_banner );
        gv = view.findViewById( R.id.gv );
        djMore = view.findViewById( R.id.dj_more );
        djXw = view.findViewById( R.id.dj_xw );
        yuan1 = view.findViewById( R.id.yuan1 );
        yuan2 = view.findViewById( R.id.yuan2 );
        yuan3 = view.findViewById( R.id.yuan3 );

        djMore.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent( getActivity(), NewsAt.class );
                intent.putExtra( "Link", "/prod-api/api/park/press/category/list" );
                intent.putExtra( "info", "dj" );
                intent.putExtra( "NewsLink", "/prod-api/api/park/press/press/list?type=" );
                intent.putExtra( "LikeLink", "/prod-api/api/park/press/press/like/" );
                startActivity( intent );
            }
        } );

        for (int i = 0; i < 3; i++) {
            ImageView imageView = new ImageView( getActivity() );
            imageView.setScaleType( ImageView.ScaleType.FIT_XY );
            banner.add( imageView );
        }
        Glide.with( getActivity() )
                .load( R.drawable.djbanner1 )
                .transform( new RoundedCorners( 15 ) )
                .into( banner.get( 0 ) );
        Glide.with( getActivity() )
                .load( R.drawable.djbanner2 )
                .transform( new RoundedCorners( 15 ) )
                .into( banner.get( 1 ) );
        Glide.with( getActivity() )
                .load( R.drawable.djbanner3 )
                .transform( new RoundedCorners( 15 ) )
                .into( banner.get( 2 ) );
        djBanner.setAdapter( new ViewPagerAdapter( banner ) );
        djBanner.addOnPageChangeListener( new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                switch (position) {
                    case 0:
                        yuan1.setBackgroundResource( R.drawable.select_yuan );
                        yuan2.setBackgroundResource( R.drawable.no_select_yuan );
                        yuan3.setBackgroundResource( R.drawable.no_select_yuan );
                        break;
                    case 1:
                        yuan1.setBackgroundResource( R.drawable.no_select_yuan );
                        yuan2.setBackgroundResource( R.drawable.select_yuan );
                        yuan3.setBackgroundResource( R.drawable.no_select_yuan );
                        break;
                    case 2:
                        yuan1.setBackgroundResource( R.drawable.no_select_yuan );
                        yuan2.setBackgroundResource( R.drawable.no_select_yuan );
                        yuan3.setBackgroundResource( R.drawable.select_yuan );
                        break;
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        } );
        timer.schedule( timerTask, 3000, 3000 );

        Collections.addAll( icons, R.drawable.ic_zl, R.drawable.ic_wddb,
                R.drawable.ic_sq, R.drawable.ic_dajy,
                R.drawable.ic_sc, R.drawable.ic_jf,
                R.drawable.ic_sy, R.drawable.ic_gd );
        Collections.addAll( titles, "档案资料", "待办事项", "党员管理", "档案借阅", "档案上传", "党员缴费", "党员审阅", "更多服务" );
        gv.setAdapter( new gvAdapter() );
        gv.setOnItemClickListener( new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity( new Intent( getActivity(), NullAt.class ) );
            }
        } );
        //获取新闻列表
        new Thread( new Runnable() {
            @Override
            public void run() {
                final String get = Tools.Get( "/prod-api/api/park/press/press/list?type=13", "" );
                if (get != null && getActivity() != null) {
                    getActivity().runOnUiThread( new Runnable() {
                        @Override
                        public void run() {
                            //新闻Bean类
                            NewsBean newsBean = new Gson().fromJson( get, NewsBean.class );
                            objects.addAll( newsBean.getRows() );
                            xwListAdapter = new NewsListAdapter( getActivity(), objects,"/prod-api/api/park/press/press/like/" );
                            xwListAdapter.setXwListAdapter( xwListAdapter );
                            djXw.setAdapter( xwListAdapter );
                        }
                    } );
                }
            }
        } ).start();
        return view;
    }

    TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            if (getActivity() != null) {
                getActivity().runOnUiThread( new Runnable() {
                    @Override
                    public void run() {
                        pos = djBanner.getCurrentItem();
                        pos++;
                        djBanner.setCurrentItem( pos );
                        if (pos == banner.size()) {
                            pos = 0;
                            djBanner.setCurrentItem( pos );
                        }
                    }
                } );
            }
        }
    };

    class gvAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return icons.size();
        }

        @Override
        public Object getItem(int position) {
            return icons.get( position );
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View inflate = getLayoutInflater().inflate( R.layout.card_item_layout, null );
            ImageView icon = inflate.findViewById( R.id.icon );
            TextView text = inflate.findViewById( R.id.text );
            icon.setImageResource( icons.get( position ) );
            text.setText( titles.get( position ) );

            return inflate;
        }
    }

}